package com.jtpay.sdk.util;

import android.app.Activity;

import java.util.Stack;

/**
 * Created by Steven on 2016/6/30 0030.
 */

public class ActivityManager {
    private ActivityManager() {

    }

    private Stack<Activity> mStack = new Stack<>();

    private static class Holder {
        private static ActivityManager mInstance = new ActivityManager();
    }

    public static ActivityManager getInstance() {
        return Holder.mInstance;
    }

    public void addActivity(Activity activity) {
        if (!mStack.contains(activity)) {
            mStack.add(activity);
        }
    }

    public void finishActivity(Activity activity) {
        if (mStack.contains(activity)) {
            mStack.remove(activity);
            activity.finish();
        }
    }

    public void finishActivity(Class<?> cls) {
        for (Activity activity : mStack) {
            if (activity.getClass().equals(cls)) {
                mStack.remove(activity);
                activity.finish();
            }
        }
    }

    public void finishAllActivitys() {
        int size = mStack.size();
        JftLogUtil.log("finishAllActivitys size = " + size);
        for (Activity activity : mStack) {
            mStack.remove(activity);
            activity.finish();
        }
//        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

}
